package com.txhy.certificatechain.ui.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.txhy.certificatechain.R;
import com.txhy.certificatechain.utils.BitmapUtil;
import com.txhy.certificatechain.utils.CommUtils;
import com.txhy.certificatechain.utils.UIUtils;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2018-2030, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: DiaLogUtils.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述: 身份证上传对话框工具类
 * 创建时间: 2018/9/20 17:54
 * 更新时间：2018/9/20 17:54
 * -----------------------------------------------------------------
 * 2018/9/20 17:54: 创建 DiaLogUtils.java (侯建军);
 * -----------------------------------------------------------------
 */
public class DiaLogUtils {

    private static Dialog mDialog;
    //身份证上传的提示dialog
    public static Dialog showCardDialog(Context context) {
        hideCardDialog();
        mDialog = new Dialog(context, R.style.MyDialog);
        LinearLayout root = (LinearLayout) LayoutInflater.from(context).inflate(
                R.layout.dialog_card, null);
        root.findViewById(R.id.tv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideCardDialog();
            }
        });

        mDialog.setContentView(root);
        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        mDialog.setCancelable(true);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        lp.x = 0; // 新位置X坐标
        lp.y = -20; // 新位置Y坐标
        lp.width = ((int) context.getResources().getDisplayMetrics().widthPixels) - CommUtils.getScreenWidth(context)/5; // 宽度
        root.measure(0, 0);
        lp.height = root.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        dialogWindow.setAttributes(lp);
        mDialog.show();
        return mDialog;
    }
    /**
     * 隐藏身份提示
     */
    public static void hideCardDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }


    /**
     * 二维码提示dialog
     * @param context
     * @param qrCode
     * @return
     */
    public static Dialog showErCodeDialog(Context context, String qrCode) {
        hideErCodeDialog();
        mDialog = new Dialog(context, R.style.MyDialog);
        LinearLayout root = (LinearLayout) LayoutInflater.from(context).inflate(
                R.layout.dialog_ercode, null);
        root.findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideErCodeDialog();
            }
        });

        ImageView img = root.findViewById(R.id.img_code);
        img.setImageBitmap(BitmapUtil.base64ToBitmap(qrCode));

        mDialog.setContentView(root);
        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        mDialog.setCancelable(true);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        lp.x = 0; // 新位置X坐标
        lp.y = -20; // 新位置Y坐标
        lp.width = ((int) context.getResources().getDisplayMetrics().widthPixels) - CommUtils.getScreenWidth(context)/3; // 宽度
        root.measure(0, 0);
        lp.height = root.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        dialogWindow.setAttributes(lp);
        mDialog.show();
        return mDialog;
    }


    /**
     * 隐藏二维码提示
     */
    public static void hideErCodeDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    private static TextView tv_one, tv_two, tv_three, tv_four;
    private static ImageView img_one, img_two, img_three, img_four;
    private static LinearLayout ll_one, ll_two, ll_three, ll_four;

    /**
     * 取消订单展示
     * @param context
     * @return
     */
    public static Dialog showCancleOrderDialog(Context context) {
        hideCancleOrderDialog();
        mDialog = new Dialog(context, R.style.MyDialog);
        LinearLayout root = (LinearLayout) LayoutInflater.from(context).inflate(
                R.layout.item_cancle_order, null);
        tv_one = root.findViewById(R.id.tv_one);
        tv_two = root.findViewById(R.id.tv_two);
        tv_three = root.findViewById(R.id.tv_three);
        tv_four = root.findViewById(R.id.tv_four);

        img_one = root.findViewById(R.id.img_one);
        img_two = root.findViewById(R.id.img_two);
        img_three = root.findViewById(R.id.img_three);
        img_four = root.findViewById(R.id.img_four);

        ll_one = root.findViewById(R.id.ll_one);
        ll_two = root.findViewById(R.id.ll_two);
        ll_three = root.findViewById(R.id.ll_three);
        ll_four = root.findViewById(R.id.ll_four);

        root.findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideCancleOrderDialog();
            }
        });

        ll_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelect();
                ll_one.setBackgroundResource(R.drawable.editbg_select);
                tv_one.setTextColor(UIUtils.getColor(R.color.ed584c));
                img_one.setVisibility(View.VISIBLE);
            }
        });
        ll_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelect();
                ll_two.setBackgroundResource(R.drawable.editbg_select);
                tv_two.setTextColor(UIUtils.getColor(R.color.ed584c));
                img_two.setVisibility(View.VISIBLE);
            }
        });
        ll_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelect();
                ll_three.setBackgroundResource(R.drawable.editbg_select);
                tv_three.setTextColor(UIUtils.getColor(R.color.ed584c));
                img_three.setVisibility(View.VISIBLE);
            }
        });
        ll_four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelect();
                ll_four.setBackgroundResource(R.drawable.editbg_select);
                tv_four.setTextColor(UIUtils.getColor(R.color.ed584c));
                img_four.setVisibility(View.VISIBLE);
            }
        });
        root.findViewById(R.id.tv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideCancleOrderDialog();
            }
        });
        root.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideCancleOrderDialog();
            }
        });

        mDialog.setContentView(root);
        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        mDialog.setCancelable(true);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        lp.x = 0; // 新位置X坐标
        lp.y = -20; // 新位置Y坐标
        lp.width = ((int) context.getResources().getDisplayMetrics().widthPixels) - CommUtils.getScreenWidth(context)/10; // 宽度
        root.measure(0, 0);
        lp.height = root.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        dialogWindow.setAttributes(lp);
        mDialog.show();
        return mDialog;
    }


    /**
     * 隐藏取消订单
     */
    public static void hideCancleOrderDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    /**
     * 设置选择
     */
    private static void setSelect() {
        tv_one.setTextColor(UIUtils.getColor(R.color.text666));
        tv_two.setTextColor(UIUtils.getColor(R.color.text666));
        tv_three.setTextColor(UIUtils.getColor(R.color.text666));
        tv_four.setTextColor(UIUtils.getColor(R.color.text666));

        img_one.setVisibility(View.INVISIBLE);
        img_two.setVisibility(View.INVISIBLE);
        img_three.setVisibility(View.INVISIBLE);
        img_four.setVisibility(View.INVISIBLE);

        ll_one.setBackgroundResource(R.drawable.edit_bg);
        ll_two.setBackgroundResource(R.drawable.edit_bg);
        ll_three.setBackgroundResource(R.drawable.edit_bg);
        ll_four.setBackgroundResource(R.drawable.edit_bg);
    }


    /**
     * 支付的界面
     * @param context
     * @return
     */
    public static Dialog showPayDialog(Context context) {
        hideCardDialog();
        mDialog = new Dialog(context, R.style.MyDialog);
        LinearLayout root = (LinearLayout) LayoutInflater.from(context).inflate(
                R.layout.dialog_pay, null);
        root.findViewById(R.id.tv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideCardDialog();
            }
        });

        mDialog.setContentView(root);
        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        mDialog.setCancelable(true);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        lp.x = 0; // 新位置X坐标
        lp.y = -20; // 新位置Y坐标
        lp.width = ((int) context.getResources().getDisplayMetrics().widthPixels); // 宽度
        root.measure(0, 0);
        lp.height = root.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        dialogWindow.setAttributes(lp);
        mDialog.show();
        return mDialog;
    }


    /**
     * 隐藏支付的界面
     */
    public static void hidePayDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }
}

